home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro22 / cbsetkcu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  3.1 KB  |  140 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbsetkcu.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stdlib.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13. #include <btree.h>
  14. #include <lseq.h>
  15.  
  16. /* local headers */
  17. #include "cbase_.h"
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      cbsetkcur - set cbase key cursor
  22.  
  23. SYNOPSIS
  24.      #include <cbase.h>
  25.  
  26.      int cbsetkcur(cbp, field, cbkposp)
  27.      cbase_t *cbp;
  28.      int field;
  29.      const cbkpos_t *cbkposp;
  30.  
  31. DESCRIPTION
  32.      The cbsetkcur function sets the position of a key cursor of the
  33.      specified field in cbase cbp to the value pointed to by cbkposp.
  34.      If cbkposp is the NULL pointer, the key cursor is set to null.
  35.      The record cursor of cbp is positioned to the record associated
  36.      with that key.  Other key cursors are not affected.
  37.  
  38.      cbsetkcur will fail if one or more of the following is true:
  39.  
  40.      [EINVAL]       cbp is not a valid cbase pointer.
  41.      [EINVAL]       field is not a valid field number for cbase cbp.
  42.      [CBELOCK]      cbp is not locked.
  43.      [CBENOPEN]     cbp is not open.
  44.  
  45. SEE ALSO
  46.      cbgetkcur, cbsetrcur.
  47.  
  48. DIAGNOSTICS
  49.      Upon successful completion, a value of 0 is returned.  Otherwise,
  50.      a value of -1 is returned, and errno set to indicate the error.
  51.  
  52. ------------------------------------------------------------------------------*/
  53. int cbsetkcur(cbp, field, cbkposp)
  54. cbase_t *cbp;
  55. int field;
  56. const cbkpos_t *cbkposp;
  57. {
  58.     void *buf = NULL;
  59.     cbrpos_t cbrpos = NIL;
  60.     lspos_t lspos = NIL;
  61.     btpos_t btpos;
  62.  
  63.     /* initialize automatic aggregates */
  64.     memset(&btpos, 0, sizeof(btpos));
  65.  
  66.     /* validate arguments */
  67.     if (!cb_valid(cbp)) {
  68.         errno = EINVAL;
  69.         return -1;
  70.     }
  71.  
  72.     /* check if not open */
  73.     if (!(cbp->flags & CBOPEN)) {
  74.         errno = CBENOPEN;
  75.         return -1;
  76.     }
  77.  
  78.     /* validate arguments */
  79.     if (field < 0 || field >= cbp->fldc) {
  80.         errno = EINVAL;
  81.         return -1;
  82.     }
  83.  
  84.     /* check if not locked */
  85.     if (!(cbp->flags & CBLOCKS)) {
  86.         errno = CBELOCK;
  87.         return -1;
  88.     }
  89.  
  90.     /* set key cursor position */
  91.     if (cbkposp == NULL) {
  92.         if (btsetcur(cbp->btpv[field], NULL) == -1) {
  93.             CBEPRINT;
  94.             return -1;
  95.         }
  96.     } else {
  97.         memcpy(&btpos, cbkposp, sizeof(btpos));
  98.         if (btsetcur(cbp->btpv[field], &btpos) == -1 ) {
  99.             CBEPRINT;
  100.             return -1;
  101.         }
  102.     }
  103.  
  104.     /* check if key cursor is null */
  105.     if (btcursor(cbp->btpv[field]) == NULL) {
  106.         if (lssetcur(cbp->lsp, NULL) == -1) {
  107.             CBEPRINT;
  108.             return -1;
  109.         }
  110.         errno = 0;
  111.         return 0;
  112.     }
  113.  
  114.     /* get record position */
  115.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  116.     if (btgetk(cbp->btpv[field], buf) == -1) {
  117.         CBEPRINT;
  118.         free(buf);
  119.         return -1;
  120.     }
  121.     memcpy(&cbrpos, ((char *)buf + cbp->fldv[field].len), sizeof(cbrpos));
  122.     free(buf);
  123.     buf = NULL;
  124.  
  125.     /* set record cursor position */
  126.     lspos = cbrpos;
  127.     if (lspos == NIL) {
  128.         CBEPRINT;
  129.         errno = CBEPANIC;
  130.         return -1;
  131.     }
  132.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  133.         CBEPRINT;
  134.         return -1;
  135.     }
  136.  
  137.     errno = 0;
  138.     return 0;
  139. }
  140.